package com.ruoyi.order.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.order.domain.WxFormRecord;
import com.ruoyi.order.domain.WxFormRecordItem;
import com.ruoyi.order.mapper.WxFormRecordMapper;
import com.ruoyi.order.service.IWxFormRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 记录表单数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-04
 */
@Service
public class WxFormRecordServiceImpl implements IWxFormRecordService
{
    @Autowired
    private WxFormRecordMapper wxFormRecordMapper;

    /**
     * 查询记录表单数据
     * 
     * @param id 记录表单数据主键
     * @return 记录表单数据
     */
    @Override
    public WxFormRecord selectWxFormRecordById(Long id)
    {
        return wxFormRecordMapper.selectWxFormRecordById(id);
    }

    @Override
    public WxFormRecord selectWxFormRecordByOrderNum(Long uid, Long orderType, String orderNum) {
        return wxFormRecordMapper.selectWxFormRecordByOrderNum(uid, orderType, orderNum);
    }

    /**
     * 查询记录表单数据列表
     * 
     * @param wxFormRecord 记录表单数据
     * @return 记录表单数据
     */
    @Override
    public List<WxFormRecord> selectWxFormRecordList(WxFormRecord wxFormRecord)
    {
        return wxFormRecordMapper.selectWxFormRecordList(wxFormRecord);
    }

    /**
     * 新增记录表单数据
     * 
     * @param wxFormRecord 记录表单数据
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWxFormRecord(WxFormRecord wxFormRecord)
    {
        wxFormRecord.setCreateTime(DateUtils.getNowDate());
        int rows = wxFormRecordMapper.insertWxFormRecord(wxFormRecord);
        insertWxFormRecordItem(wxFormRecord);
        return rows;
    }

    /**
     * 修改记录表单数据
     * 
     * @param wxFormRecord 记录表单数据
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWxFormRecord(WxFormRecord wxFormRecord)
    {
        wxFormRecordMapper.deleteWxFormRecordItemByFormRecordId(wxFormRecord.getId());
        insertWxFormRecordItem(wxFormRecord);
        return wxFormRecordMapper.updateWxFormRecord(wxFormRecord);
    }

    @Transactional
    public int updateOnlWxFormRecord(WxFormRecord wxFormRecord)
    {
        return wxFormRecordMapper.updateWxFormRecord(wxFormRecord);
    }

    /**
     * 批量删除记录表单数据
     * 
     * @param ids 需要删除的记录表单数据主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxFormRecordByIds(Long[] ids)
    {
        wxFormRecordMapper.deleteWxFormRecordItemByFormRecordIds(ids);
        return wxFormRecordMapper.deleteWxFormRecordByIds(ids);
    }

    /**
     * 删除记录表单数据信息
     * 
     * @param id 记录表单数据主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxFormRecordById(Long id)
    {
        wxFormRecordMapper.deleteWxFormRecordItemByFormRecordId(id);
        return wxFormRecordMapper.deleteWxFormRecordById(id);
    }

    @Override
    public int editRecordItem(List<WxFormRecordItem> wxFormRecordItemList) {
        int count = 0;
        for (WxFormRecordItem wxFormRecordItem : wxFormRecordItemList) {
            wxFormRecordItem.setUpdateTime(DateUtils.getNowDate());
            count+=wxFormRecordMapper.updateWxFormRecordItem(wxFormRecordItem);
        }
        return count;
    }

    /**
     * 新增用户填写完单后存储的项目数据值信息
     * 
     * @param wxFormRecord 记录表单数据对象
     */
    public void insertWxFormRecordItem(WxFormRecord wxFormRecord)
    {
        List<WxFormRecordItem> wxFormRecordItemList = wxFormRecord.getWxFormRecordItemList();
        Long id = wxFormRecord.getId();
        if (StringUtils.isNotNull(wxFormRecordItemList))
        {
            List<WxFormRecordItem> list = new ArrayList<WxFormRecordItem>();
            for (WxFormRecordItem wxFormRecordItem : wxFormRecordItemList)
            {
                wxFormRecordItem.setCreateTime(DateUtils.getNowDate());
                wxFormRecordItem.setFormRecordId(id);
                list.add(wxFormRecordItem);
            }
            if (list.size() > 0)
            {
                wxFormRecordMapper.batchWxFormRecordItem(list);
            }
        }
    }
}
